﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Framework;
using WebAppPlanProduccion.Code;
using WebAppPlanProduccion.Code.Security;

namespace WebAppPlanProduccion
{
    public partial class SiteMaster : BaseMasterPage
    {
        public override void PreLoad(List<string> operations) 
        {
            base.PreLoad(operations);
            ExceptionManager.Instance.WebMessager = this;
            this.txtMessages.Text = string.Empty;           
            BuildMenu();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //ExceptionManager.Instance.WebMessager = this;
            //this.txtMessages.Text = string.Empty;
            //this.pnlMessages.Visible = false;
            //BuildMenu();
        }

        public override void ShowMessage(string message, WebMessageType type)
        {
            ShowMessage("Mensaje", message, type);
        }

        public override void ShowMessage(string title, string message, WebMessageType type)
        {
            this.pnlMessages.CssClass = "pnlMessagesClass displayBlock";
            this.txtErrorTitle.Text = title;
            this.txtMessages.Text = message;
        }

        public override void MakeMenuVisible(bool visible)
        {
            this.NavigationMenu.Visible = visible;
        }        

        public override void BuildMenu()
        {
            if (this.NavigationMenu.Visible)
            {
                if (HasPermissionFor(PlanProdSecurity.PEDIDOCOMERCIAL_VER))
                {
                    MenuItem PedidoComercialList = new MenuItem()
                    {
                        NavigateUrl = "~/App/PedidoComercialList.aspx",
                        Text = "Pedidos comerciales"
                    };

                    this.NavigationMenu.Items.Add(PedidoComercialList);
                }

                if (HasPermissionFor(PlanProdSecurity.PRESUPUESTOPRODUCCION_VER))
                {

                    MenuItem PresupuestoProduccionList = new MenuItem()
                     {
                         NavigateUrl = "~/App/PresupuestoProduccionList.aspx",
                         Text = "Presupuestos de Producción"
                     };
                    this.NavigationMenu.Items.Add(PresupuestoProduccionList);
                }


                if (HasPermissionFor(PlanProdSecurity.PLANPRODUCCION_VER))
                {
                    MenuItem PlanProduccionList = new MenuItem()
                     {
                         NavigateUrl = "~/App/PlanProduccionList.aspx",
                         Text = "Planes de Producción"
                     };

                    if (HasPermissionFor(PlanProdSecurity.PLANPRODUCCION_DESVIO_VER))
                    {
                        MenuItem DesvioList = new MenuItem()
                        {
                            NavigateUrl = "~/App/DesvioList.aspx",
                            Text = "Ver Desvíos"
                        };

                        PlanProduccionList.ChildItems.Add(DesvioList);
                    }

                    this.NavigationMenu.Items.Add(PlanProduccionList);
                }

                MenuItem DatosMaestro = new MenuItem()
                {
                    Text = "Datos Maestro",
                    Selectable = false
                };
                bool agregarDatosMaestro = false;

                if (HasPermissionFor(PlanProdSecurity.TEMPORADA_VER))
                {
                    MenuItem TemporadaList = new MenuItem()
                    {
                        NavigateUrl = "~/App/TemporadaList.aspx",
                        Text = "Temporadas"
                    };

                    DatosMaestro.ChildItems.Add(TemporadaList);
                    agregarDatosMaestro = true;
                }

                if (HasPermissionFor(PlanProdSecurity.DESTINO_VER))
                {
                    MenuItem DestinoList = new MenuItem()
                    {
                        NavigateUrl = "~/App/DestinoList.aspx",
                        Text = "Destinos"
                    };

                    DatosMaestro.ChildItems.Add(DestinoList);
                    agregarDatosMaestro = true;
                }

                if (HasPermissionFor(PlanProdSecurity.CAUSADESVIO_VER))
                {
                    MenuItem CausaDesvioList = new MenuItem()
                    {
                        NavigateUrl = "~/App/CausaDesvioList.aspx",
                        Text = "Causa de Desvios"
                    };

                    DatosMaestro.ChildItems.Add(CausaDesvioList);
                    agregarDatosMaestro = true;
                }

                if (HasPermissionFor(PlanProdSecurity.PRODUCTO_VER))
                {
                    MenuItem ProductoList = new MenuItem()
                    {
                        NavigateUrl = "~/App/ProductoList.aspx",
                        Text = "Productos"
                    };

                    DatosMaestro.ChildItems.Add(ProductoList);
                    agregarDatosMaestro = true;
                }

                if (HasPermissionFor(PlanProdSecurity.PROVEEDOR_VER))
                {
                    MenuItem ProveedorList = new MenuItem()
                    {
                        NavigateUrl = "~/App/ProveedorList.aspx",
                        Text = "Proveedores"
                    };

                    DatosMaestro.ChildItems.Add(ProveedorList);
                    agregarDatosMaestro = true;
                }

                if (agregarDatosMaestro)
                {
                    this.NavigationMenu.Items.Add(DatosMaestro);
                }

                //if (OperationsForUser.Where(o => o == PlanProdSecurity.CAUSADESVIO_VER).Count() > 0)
                //{
                    MenuItem Reportes = new MenuItem()
                    {
                        Text = "Reportes"
                    };

                    MenuItem RegPlanInicial = new MenuItem()
                    {
                        NavigateUrl = "/Reports/RegPlanInicial.aspx",
                        Text = "Plan Inicial"
                    };

                    Reportes.ChildItems.Add(RegPlanInicial);

                    this.NavigationMenu.Items.Add(Reportes);
                //}
            }
        }
    }
}
